﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Phone_Remote.ViewModels;
using System.Net.Sockets;
using System.Net;

namespace Phone_Remote
{
    public class MainViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public static ObservableCollection<BlockItemViewModel> Controllables { get; private set; }
        public static ObservableCollection<BlockItemViewModel> Environments { get; private set; }
        public static ObservableCollection<TextBoxViewModel> Restricted { get; private set; }



        public MainViewModel()
        {
            Controllables = new ObservableCollection<BlockItemViewModel>();
            Environments = new ObservableCollection<BlockItemViewModel>();
            Restricted = new ObservableCollection<TextBoxViewModel>();

        }

        private string _sampleProperty = "Sample Runtime Property Value";
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding
        /// </summary>
        /// <returns></returns>
        public string SampleProperty
        {
            get
            {
                return _sampleProperty;
            }
            set
            {
                if (value != _sampleProperty)
                {
                    _sampleProperty = value;
                    NotifyPropertyChanged("SampleProperty");
                }
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData()
        {

            for (int i = 1; i <= 12; i++)
            {
                Rectangle r = new Rectangle();
                r.Height = 70;
                r.Width = 30;
                SolidColorBrush b = new SolidColorBrush();
                b.Color = Colors.Red;
                r.Fill = b;

                String name;
                if (i < 11)
                    name = "V" + i;
                else if (i == 11)
                    name = "VR";
                else
                    name = "VC";
                Controllables.Add(new BlockItemViewModel() { Name = name, StatusBlock = r });
            }


            for (int i = 1; i <= 12; i++)
            {
                Rectangle r = new Rectangle();
                r.Height = 70;
                r.Width = 30;
                SolidColorBrush b = new SolidColorBrush();
                b.Color = Colors.Red;
                r.Fill = b;

                String name;
                if (i < 11)
                    name = "H" + i;
                else if (i == 11)
                    name = "HR";
                else
                    name = "HC";
                Controllables.Add(new BlockItemViewModel() { Name = name, StatusBlock = r });
            }





            Restricted.Add(new TextBoxViewModel() { Name = "LMOC", RestrectedText = "" });
            Restricted.Add(new TextBoxViewModel() { Name = "Music", RestrectedText = "" });
            Restricted.Add(new TextBoxViewModel() { Name = "Kiosk", RestrectedText = "" });
            Restricted.Add(new TextBoxViewModel() { Name = "Phone", RestrectedText = "" });
            Restricted.Add(new TextBoxViewModel() { Name = "Bollards", RestrectedText = "" });
            Restricted.Add(new TextBoxViewModel() { Name = "Sequences", RestrectedText = "" });


            this.IsDataLoaded = true;
        }

        internal static void SetEnviron(SolidColorBrush c1, SolidColorBrush c2, SolidColorBrush c3, SolidColorBrush c4)
        {
            Rectangle r1 = new Rectangle();
            Rectangle r2 = new Rectangle();
            Rectangle r3 = new Rectangle();
            Rectangle r4 = new Rectangle();
            r1.Height = 70;
            r1.Width = 30;
            r2.Height = 70;
            r2.Width = 30;
            r3.Height = 70;
            r3.Width = 30;
            r4.Height = 70;
            r4.Width = 30;
            r1.Fill = c1;
            r2.Fill = c2;
            r3.Fill = c3;
            r4.Fill = c4;

            Environments.Add(new BlockItemViewModel() { Name = "Manhole Cover Closed", StatusBlock = r1 });
            Environments.Add(new BlockItemViewModel() { Name = "Sidewalk Water Level OK", StatusBlock = r2 });
            Environments.Add(new BlockItemViewModel() { Name = "South End HW Disable", StatusBlock = r3 });
            Environments.Add(new BlockItemViewModel() { Name = "North End HW Disable", StatusBlock = r4 });

            /*
            for (int i = 0; i < 5; i++)
            {
                String bollardName = "B" + i;
                this.Environments.Add(new BlockItemViewModel() { Name = bollardName, StatusBlock = r });
            }

            for (int i = 0; i < 5; i++)
            {
                String bollardName = "P" + i;
                this.Environments.Add(new BlockItemViewModel() { Name = bollardName, StatusBlock = r });
            }
            */
        }

        public static void SetRestricted(string valves, int index)
        {
            TextBoxViewModel tbvm = Restricted[index];
            Restricted.RemoveAt(index);
            tbvm.RestrectedText = valves;
            Restricted.Insert(index, tbvm);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        internal static BlockItemViewModel GetControllables(string valve)
        {
            foreach (BlockItemViewModel b in Controllables)
            {
                if (b.Name == valve)
                    return b;
            }

            return null;
        }

        internal static void SetRectColor(Color color, string valve)
        {
            BlockItemViewModel bivm = MainViewModel.GetControllables(valve);
            int index = Controllables.IndexOf(bivm);
            Controllables.RemoveAt(index);
            SolidColorBrush brush = new SolidColorBrush();
            brush.Color = color;
            bivm.RFill = brush;
            Controllables.Insert(index, bivm);
        }

        internal static List<List<string>> getRestrictedValves()
        {
            List<List<String>> global = new List<List<string>>();
            List<String> local;

            foreach (TextBoxViewModel tbvm in Restricted)
            {
                local = new List<string>();
                string[] arr = tbvm.RestrectedText.Split(' ');
                for (int i = 0; i < arr.Length; i++)
                {
                    local.Add(arr[i]);
                }
                global.Add(local);
            }

            return global;
        }

        internal static void UpdateText(TextBox tb)
        {
            foreach (TextBoxViewModel tbvm in Restricted)
            {
                if (tbvm.Name == tb.Name)
                {
                    tbvm.RestrectedText = tb.Text;
                }
            }
        }
    }
}